package com.fanqie.springboot2.redis;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author php
 * @date 2019/4/5
 */
@Component
@Slf4j
public class RedisComponent {
    private final RedisTemplate redisTemplate;


    @Autowired
    public RedisComponent(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }


    public void options() {
        //键值对存储
        this.redisTemplate.opsForValue().set("name", "张三");
        String name = (String) this.redisTemplate.opsForValue().get("name");
        log.info(name);
        //list存储
        this.redisTemplate.opsForList().leftPushAll("names", "李四", "王五", "赵六");
        List names = this.redisTemplate.opsForList().range("names", 0, 3);
        log.info(names.toString());
        //hash使用
        Map<String, String> map = new HashMap<>(4);
        map.put("name", "秦始皇");
        map.put("age", "未知");
        map.put("sex", "男");
        this.redisTemplate.opsForHash().putAll("HASH_PERSON", map);
        String s = (String) this.redisTemplate.opsForHash().get("HASH_PERSON", "name");
        log.info(s);
    }


}
